﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.HitachiServiceRef;

namespace HitachiLib.Data
{
    public class ViewItem : INotifyPropertyChanged
    {
        private Project project;
        private bool common;
        private bool selected;

        public ViewItem(Project p, bool c)
        {
            project = p;
            common = c;
            selected = false;
        }

        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public bool Common
        {
            get { return common; }
        }

        public Project Project
        {
            get { return project; }
        }

        // Project Properties
        public ProjectData Data
        {
            get { return project.Data; }
        }

        public String Name
        {
            get { return Data.Name; }
        }

        public int Id
        {
            get { return Data.ID; }
        }

        public Dictionary<int, Schedule> PlanSchedule
        {
            get { return project.PlanSchedule; }
        }

        public Schedule CommonSchedule
        {
            get
            {
                if (PlanSchedule.ContainsKey(Schedule.HalfMonth)) return PlanSchedule[Schedule.HalfMonth];
                return null;
            }
        }
        public Schedule YearlySchedule
        {
            get
            {
                if (PlanSchedule.ContainsKey(Schedule.WholeYear)) return PlanSchedule[Schedule.WholeYear];
                return null;
            }
        }

        public Schedule Schedule
        {
            get
            {
                if (common)
                    return CommonSchedule;
                else
                    return YearlySchedule;
            }
        }
        // Schedule Properties

        public int ActionType
        {
            get { return Schedule.Data.ActionType; }
        }
        public Project Proj
        {
            get { return project; }
        }
        public String PrjName
        {
            get { return Proj.Data.CustomName; }
        }
        public String PrjDevID
        {
            get { return Proj.Data.DevID; }
        }
        public String PrjContract
        {
            get { return Proj.Data.ContractType; }
        }
        public int HalfYearDiff
        {
            get
            {
                DateTime today = DateTime.Today;
                DateTime target_date = Utils.DateUtils.GetDate(Proj.Data.ActionDate);
                target_date = target_date.AddMonths(6);
                DateTime next_date = Utils.DateUtils.GetNearest(target_date, Schedule.WholeYear);
                TimeSpan ts = next_date.Subtract(today);
                int d = (int)ts.TotalDays;
                return d;
            }
        }
        public String HalfYearDiffStr
        {
            get
            {
                int d = HalfYearDiff;
                if (d < -45 || d > 45)
                {
                    return "";
                }
                else
                    return "" + d;
            }
        }
        public int YearDiff
        {
            get
            {
                DateTime today = DateTime.Today;
                DateTime target_date = Utils.DateUtils.GetDate(Proj.Data.ActionDate);
                DateTime next_date = Utils.DateUtils.GetNearest(target_date, Schedule.WholeYear);
                TimeSpan ts = next_date.Subtract(today);
                int d = (int)ts.TotalDays;
                return d;
            }
        }
        public String YearDiffStr
        {
            get
            {
                int d = YearDiff;
                if (d < -45 || d > 45)
                {
                    return "";
                }
                else
                    return "" + d;
            }
        }
        public bool Quarter
        {
            get
            {
                DateTime today = DateTime.Today;
                DateTime target_date = Utils.DateUtils.GetDate(Proj.Data.ActionDate);
                DateTime next_date = Utils.DateUtils.GetNearest(target_date, Schedule.ThreeMonth);
                TimeSpan ts = next_date.Subtract(today);
                int d = (int)ts.TotalDays;

                if (d < 7 && d > -7)
                {
                    return true;
                }
                return false;
            }
        }
        public int AutoActionType
        {
            get
            {
                if (Schedule.Data.ActionType == Schedule.HalfMonth)
                {
                    if (Quarter)
                        return Schedule.ThreeMonth;
                    else
                        return Schedule.HalfMonth;
                }
                else
                {
                    if (YearDiff < 45 && YearDiff > -45)
                        return Schedule.WholeYear;
                    else
                        return Schedule.HalfYear;
                }

            }
        }
        public String AutoActionTypeStr
        {
            get
            {
                int t = AutoActionType;
                switch (t)
                {
                    case Schedule.WholeYear:
                        return "全年";
                    case Schedule.HalfYear:
                        return "半年";
                    case Schedule.ThreeMonth:
                        return "季度";
                    default:
                        return "常规";
                }
            }
        }
        public DayOfWeek DayWeek
        {
            get { return Schedule.PlanDate.DayOfWeek; }
        }
        public String TimeDay
        {
            get
            {
                switch (Schedule.PlanDate.Hour)
                {
                    case Schedule.TimeMorning:
                        return "上午";
                    case Schedule.TimeAfternoon:
                        return "下午";
                    default:
                        return "晚上";
                }
            }
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion

    }
}
